import { Config } from 'src/config';
import { redisUtils as redis } from 'src/utils/redis';
import { PrismaService } from 'prisma/prisma.server';

export const getUser = async (
  token: string,
  role: 'admin' | 'user' = 'admin',
): Promise<string | null> => {
  const prisma: PrismaService = new PrismaService();
  const id = +(await redis.get(`${Config.token.prefix}${token}`));
  const user = await prisma.users.findFirst({
    where: {
      id,
      role,
    },
  });
  return user?.username || '游客';
};
